package com.example.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.NewsBean;

import java.util.List;

/**
 * @Author: Ng
 * @Date: 2024/4/8
 * @Versopn: 1.0.0
 */
public class NewsAdapter extends BaseAdapter {

    private NewsBean newsBean;
    private Context context;
    private List<Fragment> fragments;

    public NewsAdapter(NewsBean newsBean, List<Fragment> fragments) {
        this.newsBean = newsBean;
        this.fragments = fragments;
    }

    @Override
    public int getCount() {
        // return newsBean!=null ? newsBean.getRows().size() : 0;
        return newsBean.getRows().size();
    }

    @Override
    public Object getItem(int i) {
        return newsBean.getRows().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = View.inflate(context, R.layout.news_fragment_news_item,null);
            holder = new ViewHolder();
            holder.new_img = view.findViewById(R.id.news_item_image_view);
            holder.new_title = view.findViewById(R.id.news_item_title);
            holder.new_context = view.findViewById(R.id.news_item_content);
            holder.new_date = view.findViewById(R.id.news_item_date);
            holder.likeNumber = view.findViewById(R.id.news_item_like);
            holder.viewsNumber = view.findViewById(R.id.news_item_views);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.likeNumber.setText(newsBean.getRows().get(i).getLikeNumber());
        holder.viewsNumber.setText(newsBean.getRows().get(i).getViewsNumber());
        holder.new_date.setText(newsBean.getRows().get(i).getCreateTime());
        holder.new_context.setText(newsBean.getRows().get(i).getContent());
        holder.new_title.setText(newsBean.getRows().get(i).getTitle());
        String url = "http://pic.gooooal.com/images/102422/102422663.jpg";  // APIConfig.BASE_URL + newsBean.getRows().get(i).getImgUrl();
        Glide.with(context).load(url).into(holder.new_img);
        return view;
    }

    // ListView 优化类
    class ViewHolder{
        ImageView new_img;
        TextView new_title;
        TextView new_context;
        TextView new_date;
        TextView viewsNumber;
        TextView likeNumber;
    }
}
